package cn.tedu.ivos2.vehiclerepair.service.impl;

import cn.tedu.ivos2.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos2.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos2.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos2.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos2.vehiclerepair.mapper.VehicleRepairMapper;
import cn.tedu.ivos2.vehiclerepair.pojo.dto.VehicleRepairParam;
import cn.tedu.ivos2.vehiclerepair.pojo.dto.VehicleRepairQuery;
import cn.tedu.ivos2.vehiclerepair.pojo.entity.VehicleRepair;
import cn.tedu.ivos2.vehiclerepair.pojo.vo.VehicleRepairVO;
import cn.tedu.ivos2.vehiclerepair.service.VehicleRepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class VehicleRepairServiceImpl implements VehicleRepairService {
    @Autowired
    private VehicleRepairMapper vehicleRepairMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleRepairVO> select(VehicleRepairQuery vehicleRepairQuery) {
        log.debug("S1-查询维修参数:{}",vehicleRepairQuery);
        List<VehicleRepairVO> vehicleRepairVOList=vehicleRepairMapper.selectRepair(vehicleRepairQuery);
        return vehicleRepairVOList;
    }

    @Override
    public void delete(Long id) {
        log.debug("S2-删除id:{}",id);
        vehicleRepairMapper.deleteRepair(id);
    }

    @Override
    public void save(VehicleRepairParam vehicleRepairParam) {
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setLicense(vehicleRepairParam.getLicense());
        log.debug("S3-查询车辆的id{}",vehicleQuery);
        List<VehicleVO> query = vehicleMapper.query(vehicleQuery);
        log.debug("S3-返回查询结果:{}",query);
        VehicleRepair vehicleRepair = new VehicleRepair();
        BeanUtils.copyProperties(vehicleRepairParam, vehicleRepair);
        vehicleRepair.setVehicleId(query.get(0).getId());
        if (vehicleRepairParam.getId()==null) {
            log.debug("S4-新增数据:{}",vehicleRepair);
            vehicleRepairMapper.insert(vehicleRepair);
            Vehicle vehicle = new Vehicle();
            vehicle.setId(vehicleRepair.getVehicleId());
            vehicle.setStatus("3");
            log.debug("S5-更新车辆状态:{}",vehicle);
            vehicleMapper.update(vehicle);
        }else {
            log.debug("S6-更新维修数据:{}",vehicleRepair);
            vehicleRepairMapper.updateRepair(vehicleRepair);
        }
    }

    @Override
    public VehicleRepairVO selectById(Long id) {
        return vehicleRepairMapper.selectById(id);
    }
}
